<template>
  <main-layout>
    <div class="product-detail">
      <div class="container">
        <!-- 面包屑导航 -->
        <div class="breadcrumb">
          <router-link to="/">首页</router-link> /
          <router-link to="/products">花艺产品</router-link> /
          <span>{{ product.name }}</span>
        </div>

        <!-- 商品信息 -->
        <div class="product-info">
          <!-- 左侧商品图片 -->
          <div class="product-gallery">
            <div class="main-image">
              <img :src="currentImage" :alt="product.name" />
            </div>
            <div class="thumbnail-list">
              <img
                v-for="(image, index) in product.gallery"
                :key="index"
                :src="image"
                :class="{ active: currentImage === image }"
                @click="currentImage = image"
              />
            </div>
          </div>

          <!-- 右侧商品信息 -->
          <div class="product-details">
            <h1>{{ product.name }}</h1>
            <div class="tags">
              <span v-for="tag in product.tags" :key="tag" class="tag">{{
                tag
              }}</span>
            </div>
            <div class="price">¥{{ product.price }}</div>

            <div class="description">
              <h3>商品描述</h3>
              <p>{{ product.description }}</p>
            </div>

            <div class="specifications">
              <h3>商品规格</h3>
              <ul>
                <li>花材：{{ product.specifications?.materials }}</li>
                <li>花束尺寸：{{ product.specifications?.size }}</li>
                <li>适用场景：{{ product.specifications?.occasion }}</li>
                <li>保鲜时间：{{ product.specifications?.freshness }}</li>
              </ul>
            </div>

            <div class="purchase-options">
              <div class="quantity">
                <button @click="decreaseQuantity" :disabled="quantity <= 1">
                  -
                </button>
                <input
                  type="number"
                  v-model="quantity"
                  min="1"
                  :max="product.stock"
                />
                <button
                  @click="increaseQuantity"
                  :disabled="quantity >= product.stock"
                >
                  +
                </button>
              </div>
              <div
                class="stock-info"
                :class="{ 'low-stock': product.stock < 10 }"
              >
                库存：{{ product.stock }}件
              </div>
            </div>

            <div class="action-buttons">
              <button class="add-to-cart" @click="addToCart">
                <i class="fas fa-shopping-cart"></i>
                加入购物车
              </button>
              <button class="buy-now" @click="buyNow">立即购买</button>
              <button class="favorite" @click="toggleFavorite">
                <i :class="['fa-heart', isFavorite ? 'fas' : 'far']"></i>
              </button>
            </div>

            <div class="service-info">
              <div class="service-item">
                <i class="fas fa-truck"></i>
                <span>全城配送</span>
              </div>
              <div class="service-item">
                <i class="fas fa-shield-alt"></i>
                <span>品质保证</span>
              </div>
              <div class="service-item">
                <i class="fas fa-undo"></i>
                <span>七天换</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 商品详情标签页 -->
        <div class="product-tabs">
          <div class="tab-headers">
            <div
              v-for="tab in tabs"
              :key="tab.id"
              :class="['tab-header', { active: currentTab === tab.id }]"
              @click="currentTab = tab.id"
            >
              {{ tab.name }}
            </div>
          </div>

          <div class="tab-content">
            <!-- 商品详情 -->
            <div v-if="currentTab === 'detail'" class="detail-content">
              <img
                v-for="(image, index) in product.detailImages"
                :key="index"
                :src="image"
                :alt="product.name + '详情图' + (index + 1)"
              />
            </div>

            <!-- 商品评价 -->
            <div v-if="currentTab === 'comments'" class="comments-content">
              <product-comments :product-id="product.id" />
            </div>

            <!-- 售后服务 -->
            <div v-if="currentTab === 'service'" class="service-content">
              <h3>配送说明</h3>
              <p>我们提供全城配送服务，通常在下单后24小时内送达。</p>

              <h3>退换政策</h3>
              <p>如果收到的商品有质量问题，我们支持7天无理由退换。</p>

              <h3>保养说明</h3>
              <p>为了让鲜花保持更长时间的美丽，请注意以下事项：</p>
              <ul>
                <li>避免阳光直射</li>
                <li>定期更换清水</li>
                <li>保持适宜温度</li>
                <li>远离空调出风口</li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </main-layout>
</template>

<script>
import MainLayout from "@/components/layouts/MainLayout.vue";
import ProductComments from "@/components/ProductComments.vue";

export default {
  name: "ProductDetailView",
  components: {
    MainLayout,
    ProductComments,
  },
  data() {
    return {
      product: {},
      currentImage: "",
      quantity: 1,
      isFavorite: false,
      currentTab: "detail",
      tabs: [
        { id: "detail", name: "商品详情" },
        { id: "comments", name: "商品评价" },
        { id: "service", name: "售后服务" },
      ],
    };
  },
  mounted() {
    this.initializeProduct();
  },
  methods: {
    async initializeProduct() {
      if (this.$store.state.flowers.length === 0) {
        await this.$store.dispatch("fetchFlowers");
      }

      const productId = Number(this.$route.params.id);
      const foundProduct = this.$store.state.flowers.find(
        (f) => f.id === productId
      );

      if (!foundProduct) {
        this.$router.push("/products");
        return;
      }

      this.product = foundProduct;
      this.currentImage = foundProduct.image;
      // 设置商品详情图片
      this.product.gallery = [
        this.product.image,
        require("@/assets/images/detail/3dc66de14658f7ec189c6fcf78c78d1a.jpg"),
        require("@/assets/images/detail/photo-1509719662282-b82bed65a96b.avif"),
        require("@/assets/images/detail/photo-1562690868-60bbe7293e94.avif"),
      ];

      this.product.detailImages = [
        require("@/assets/images/detail/photo-1526047932273-341f2a7631f9.avif"),
        require("@/assets/images/detail/photo-1567696153798-9111f9cd3d0d.avif"),
        require("@/assets/images/detail/photo-1613508636209-cd13d894e15b.avif"),
      ];

      this.product.specifications = {
        materials: "进口玫瑰19支",
        size: "高35cm，直径25cm",
        occasion: "送恋人、生日、纪念日",
        freshness: "5-7天",
      };
    },
    decreaseQuantity() {
      if (this.quantity > 1) {
        this.quantity--;
      }
    },
    increaseQuantity() {
      if (this.quantity < this.product.stock) {
        this.quantity++;
      }
    },
    addToCart() {
      this.$store.commit("addToCart", {
        ...this.product,
        quantity: this.quantity,
      });
    },
    buyNow() {
      this.addToCart();
      this.$router.push("/cart");
    },
    toggleFavorite() {
      this.isFavorite = !this.isFavorite;
    },
  },
};
</script>

<style scoped>
.product-detail {
  padding: 40px 0;
  background: #f8f9fa;
}

.breadcrumb {
  margin-bottom: 30px;
  color: #666;
}

.breadcrumb a {
  color: #666;
  transition: color 0.3s;
}

.breadcrumb a:hover {
  color: #ff6b6b;
}

.product-info {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 40px;
  margin-bottom: 40px;
}

.product-gallery {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.main-image {
  width: 100%;
  height: 400px;
  margin-bottom: 20px;
  overflow: hidden;
  border-radius: 4px;
}

.main-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.main-image:hover img {
  transform: scale(1.05);
}

.thumbnail-list {
  display: flex;
  gap: 10px;
}

.thumbnail-list img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.thumbnail-list img:hover {
  transform: translateY(-2px);
}

.thumbnail-list img.active {
  border: 2px solid #ff6b6b;
}

.product-details {
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.product-details h1 {
  font-size: 24px;
  color: #333;
  margin-bottom: 15px;
}

.tags {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.tag {
  padding: 4px 12px;
  background: #ff6b6b;
  color: white;
  border-radius: 4px;
  font-size: 14px;
}

.price {
  font-size: 28px;
  color: #ff6b6b;
  font-weight: bold;
  margin-bottom: 20px;
}

.description,
.specifications {
  margin-bottom: 25px;
}

.description h3,
.specifications h3 {
  font-size: 18px;
  color: #333;
  margin-bottom: 10px;
}

.description p {
  color: #666;
  line-height: 1.6;
}

.specifications ul {
  list-style: none;
  padding: 0;
}

.specifications li {
  color: #666;
  margin-bottom: 8px;
}

.purchase-options {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 25px;
}

.quantity {
  display: flex;
  align-items: center;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.quantity button {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: #666;
  font-size: 16px;
}

.quantity button:disabled {
  color: #ccc;
}

.quantity input {
  width: 50px;
  height: 36px;
  border: none;
  border-left: 1px solid #ddd;
  border-right: 1px solid #ddd;
  text-align: center;
}

.stock-info {
  color: #666;
  font-size: 14px;
}

.stock-info.low-stock {
  color: #ff6b6b;
}

.action-buttons {
  display: flex;
  gap: 15px;
  margin-bottom: 25px;
}

.action-buttons button {
  height: 44px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  transition: all 0.3s;
}

.add-to-cart {
  flex: 1;
  background: #ff6b6b;
  color: white;
}

.add-to-cart:hover {
  background: #ff5252;
}

.buy-now {
  flex: 1;
  background: #ff4757;
  color: white;
}

.buy-now:hover {
  background: #ff3344;
}

.favorite {
  width: 44px;
  background: #f8f9fa;
  color: #666;
}

.favorite:hover {
  background: #ff6b6b;
  color: white;
}

.service-info {
  display: flex;
  justify-content: space-between;
  padding-top: 25px;
  border-top: 1px solid #eee;
}

.service-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #666;
}

.service-item i {
  color: #ff6b6b;
}

.product-tabs {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.tab-headers {
  display: flex;
  background: #f8f9fa;
  border-bottom: 1px solid #eee;
}

.tab-header {
  padding: 15px 30px;
  cursor: pointer;
  transition: all 0.3s;
  color: #666;
}

.tab-header:hover {
  color: #ff6b6b;
}

.tab-header.active {
  color: #ff6b6b;
  border-bottom: 2px solid #ff6b6b;
}

.tab-content {
  padding: 30px;
}

.detail-content img {
  width: 100%;
  margin-bottom: 20px;
  border-radius: 4px;
}

.service-content h3 {
  font-size: 18px;
  color: #333;
  margin: 25px 0 15px;
}

.service-content h3:first-child {
  margin-top: 0;
}

.service-content p {
  color: #666;
  line-height: 1.6;
  margin-bottom: 15px;
}

.service-content ul {
  list-style: none;
  padding-left: 20px;
}

.service-content li {
  color: #666;
  margin-bottom: 10px;
  position: relative;
}

.service-content li::before {
  content: "•";
  color: #ff6b6b;
  position: absolute;
  left: -15px;
}

@media (max-width: 992px) {
  .product-info {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .action-buttons {
    flex-wrap: wrap;
  }

  .add-to-cart,
  .buy-now {
    width: 100%;
  }

  .favorite {
    width: 100%;
    height: 44px;
  }

  .tab-headers {
    overflow-x: auto;
    white-space: nowrap;
  }
}
</style> 